/**
 * 边框组件
 * 子元素：边框，热区
 * 
 * 边框：充满整个组件可见区域，组件内不可移动，不可调整大小。
 * 热区：固定位置大小，组件内不可移动，不可调整大小。
 * 热区内元素：照片，可调整显示属性如大小，位置，角度。
 * 
 * Author: hVenus
 * Last Modify: 2010-07-05
 */
package
{
	
	import flash.events.Event;
	import flash.events.FocusEvent;
	import flash.events.MouseEvent;
	
	import mx.containers.Canvas;
	import mx.controls.Image;
	import mx.controls.TileList;
	import mx.events.DragEvent;
	import mx.events.ResizeEvent;
	import mx.managers.DragManager;

	public class fmborder extends Canvas
	{
		/**
		 * 原始数据
		 */
		private var _ori_width:Number; //宽
		private var _ori_height:Number; //高
		private var _ori_img_original:String; //原始图片的地址
		private var _ori_thumb_url:String; //缩累
		private var _ori_img_desc:String; //说明
		private var _ori_img_id:String; //id
		private var _ori_goods_id:String; //商品编号
		private var _ori_hotarea:String; //热区数据xml格式
		private var _ori_array:Object;
		
		private var img:Image;
		
		private var flag_autoid:int = 0; //
		
		public function fmborder(args:Object)
		{
			super();
			
			this.verticalScrollPolicy = "off";
			this.horizontalScrollPolicy = "off";
			
			//保存原始数据
			this._ori_array = args;

			addBorder();
			addHotarea();

		}
		
		private function addBorder():void{
			var tmp:XML = XML(this._ori_array);
			var ncs = this.flag_autoid++;
			//添加border img
			img = new Image();
			img.name = "border_"+ncs;
			img.percentWidth = 100;
			img.percentHeight = 100;
			img.source = tmp.urlx;
			img.maintainAspectRatio = false;
			addChild(img);
			this.width = tmp.width;
			this.height = tmp.height;
		}
		
		private function addHotarea():void{
			var tmp:XML = XML(this._ori_array);
			
			//添加hotarea
			if(tmp.hotarea!=''){
				var t3:XML = XML(tmp.hot);
				//Alert.show(t3);
				for each(var ss:XML in t3.hoterea.info){
					var ncs = this.flag_autoid++;
					var ha1:hoterea1 = new hoterea1();
					ha1.name = 'hotarea'+ncs;
					ha1.id = ha1.name;
					ha1.width = ss.width;
					ha1.height = ss.height;
					ha1.rotation = ss.rotate;
					ha1.x = ss.x;
					ha1.y = ss.y;
					
					ha1.addEventListener(DragEvent.DRAG_ENTER,dragEnterHandler);
    				ha1.addEventListener(DragEvent.DRAG_DROP,dragDropHandler);
    				//ha1.addEventListener(MouseEvent.CLICK, _x);
    				//ha1.addEventListener(MouseEvent.MOUSE_OVER, function (e:MouseEvent){e.currentTarget.setStyle("borderColor","#FF6633");e.currentTarget.setStyle("borderStyle","solid");e.currentTarget.setStyle("borderThickness","4"); });
					//ha1.addEventListener(MouseEvent.MOUSE_OUT, function (e:MouseEvent){e.currentTarget.setStyle("borderStyle","none");});
    				
    				ha1.doubleClickEnabled = true;
    				//ha1.addEventListener(MouseEvent.DOUBLE_CLICK, function (e:MouseEvent){hotareadbclick(e, ha1);});
					addChild(ha1);
					
				}
				
			}
			
		}
		
		// mainid的dragEnter事件
			private function dragEnterHandler(event:DragEvent):void {
    			DragManager.acceptDragDrop(Canvas(event.currentTarget));
			}
			//鼠标释放
			private  function dragDropHandler(event:DragEvent):void {
    			var hotname = '';
    			var hta1:hoterea1; 
    			if(event.currentTarget is hoterea1){
    				hotname = event.currentTarget.name;
    				//Alert.show(hotname);
    				hta1 = this.getChildByName(hotname) as hoterea1;
    			}
    			
    			var tl:TileList = event.dragInitiator as TileList;
    			var imgarray:Object = {name: tl.selectedItem.name,
    									urlx:tl.selectedItem.urlx, 
    									width:tl.selectedItem.width,
    									height:tl.selectedItem.height,
    									thumb:tl.selectedItem.thumb,
    									idx:tl.selectedItem.idx };
    			var image:Image = new Image;
    			var htn1:String = '';
    			if(event.currentTarget is hoterea1){
    				htn1 = event.currentTarget.name;
    			}
				image.source = ((event.dragInitiator as TileList).selectedItem.urlx);
				if(tl.selectedItem.height>=hta1.height){
					image.height = hta1.height;
				}
				//image.width = hta1.width;
				//image.height = hta1.height;
				image.maintainAspectRatio = true;
				var mydate:Date = new Date();
				image.name = "imib"+mydate.getTime();
				image.id = image.name;
				hta1.removeAllChildren();
				hta1.addChild(image);
				hta1.setImgData(imgarray);
				//trace(hta1.name);
		}
		
		
		/**
		 * 鼠标在组件上方将中主设置到底层
		 */
		public function _setup():void{
			this.setChildIndex(this.img, 0);
			trace('setup');
		}
		
		/**
		 * 鼠标离开组件将中主设置到顶层
		 */
		public function _setdown():void{
			this.setChildIndex(this.img, this.numChildren-1);
			trace('setdown');
		}
	}
}